//
//  OMGGetMoreViewController.m
//  A_project2.0
//
//  Created by Tiny on 16/3/14.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGGetMoreViewController.h"
#import "OMGGetMoreTableViewCell.h"
#import "GetMoreModel.h"
@interface OMGGetMoreViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain) NSMutableArray *dataArr;      /**< tableView数组 */
@property (nonatomic, retain) UITableView *tableView;       /**< tableview */
@end

@implementation OMGGetMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"专题";
    self.dataArr = [NSMutableArray array];
    [self dataHandler];                 /**< 数据处理 */
    [self creatTableView];              /**< 创建tableView */
    [self creatUI];                     /**< MJ刷新 */
    _page = 1;
}
- (void)creatUI
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self dataHandler];
    }];
    [header setTitle:@"玩 命 加 载 中..." forState:MJRefreshStatePulling];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self dataHandler];
    }];
}
- (void)dataHandler
{
    [QPNetWorkingTool getNetWorkingTool:[[NSString stringWithFormat:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88250892208679&lon=121.5399580135169&page=%ld",(long)_page]stringByAppendingString:@"&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1"] dic:nil success:^(id result) {
        if (_page == 1) {
            [_dataArr removeAllObjects];
        }
        for (NSDictionary *dic in result[@"data"]) {
            GetMoreModel *g = [GetMoreModel modelWithDic:dic];
            [self.dataArr addObject:g];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        
    }];
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[OMGGetMoreTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
}
#pragma mark - 两种必须实现的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGGetMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];          /**< 取消tableview选中状态 */
    self.hidesBottomBarWhenPushed = YES;
    OMGWebViewViewController *wv = [[OMGWebViewViewController alloc]init];
    GetMoreModel *gm = _dataArr[indexPath.row];
    wv.urlStr = gm.url;
    [self.navigationController pushViewController:wv animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
